package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Fruit;

import java.io.*;
import java.util.ArrayList;

public class OtherFruitDao1 implements BaseFruitDao{
    private static ArrayList<Fruit> fruits = new ArrayList<>();

    static {
        try {
            BufferedReader br = new BufferedReader(new FileReader("FruitMarket\\fruits.txt"));
            String line;
            while ((line = br.readLine()) != null) {
                String[] split = line.split(System.lineSeparator());
                for (int i = 0; i < split.length; i++) {
                    String[] strings = split[i].split(",");
                    Fruit fruit = new Fruit(strings[0], strings[1], Integer.parseInt(strings[2]), Integer.parseInt(strings[3]));
                    fruits.add(fruit);
                }
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 查看所有水果对象并返回
    public Fruit[] findAllFruit() {
        Fruit[]  f= new Fruit[fruits.size()];

        for (int i = 0; i < f.length; i++) {
            f[i] = fruits.get(i);
        }
        return f;
    }
}
